package com.google.code.ojar.sendtemplatemail.app.dto;

import java.io.Serializable;
import java.util.ArrayList;
import java.util.List;
import java.util.regex.Matcher;
import java.util.regex.Pattern;
/**
 * Dto class that holds the email properties 
 * @author ottoabreu
 *
 */
public class Mail implements Serializable {
	
	
	

	/**
	 * 
	 */
	private static final long serialVersionUID = 1L;
	private List<String> toAdds;
	private List<String> bccAdds;
	private List<String> ccAdds;
	private String msg;
	private String subject;
	private List<Attachment> attachments;
	private boolean htmlMail;
	
	public void addAttachments(Attachment attch){
		if(this.attachments == null){
			this.attachments = new ArrayList<Attachment>();
		}
		this.attachments.add(attch);
	}
	public void addToAdds(String toAdd){
		this.toAdds = Mail.setStringProp(this.toAdds, toAdd);
	}
	public void addBccAdds(String bccAdd){
		this.bccAdds = Mail.setStringProp(this.bccAdds, bccAdd);
	}
	public void addCcAdds(String ccAdd){
		this.ccAdds =Mail.setStringProp(this.ccAdds, ccAdd);
	}
	
	public boolean isHtmlMail() {
		return htmlMail;
	}
	public void setHtmlMail(boolean htmlMail) {
		this.htmlMail = htmlMail;
	}
	
	public Mail(List<String> toAdds, List<String> bccAdds, List<String> ccAdds) {
		super();
		this.toAdds = toAdds;
		this.bccAdds = bccAdds;
		this.ccAdds = ccAdds;
	}
	public Mail(List<String> toAdds, List<String> bccAdds, List<String> ccAdds,
			String msg, String subject, List<Attachment> attachments) {
		super();
		this.toAdds = toAdds;
		this.bccAdds = bccAdds;
		this.ccAdds = ccAdds;
		this.msg = msg;
		this.subject = subject;
		this.attachments = attachments;
		this.htmlMail = this.isHtmlMsg();
	}
	public List<Attachment> getAttachments() {
		return attachments;
	}
	public void setAttachments(List<Attachment> attachments) {
		this.attachments = attachments;
	}
	public String getSubject() {
		String subject ="";
		if(this.subject != null){
			subject = this.subject;
		}
		return subject;
	}
	public void setSubject(String subject) {
		this.subject = subject;
	}
	
	public Mail(List<String> toAdds, List<String> bccAdds, List<String> ccAdds,
			 String subject,String msg) {
		super();
		this.toAdds = toAdds;
		this.bccAdds = bccAdds;
		this.ccAdds = ccAdds;
		this.msg = msg;
		this.subject = subject;
		this.htmlMail = this.isHtmlMsg();
	}
	public List<String> getToAdds() {
		return toAdds;
	}
	public void setToAdds(List<String> toAdds) {
		this.toAdds = toAdds;
	}
	public List<String> getBccAdds() {
		return bccAdds;
	}
	public void setBccAdds(List<String> bccAdds) {
		this.bccAdds = bccAdds;
	}
	public List<String> getCcAdds() {
		return ccAdds;
	}
	public void setCcAdds(List<String> ccAdds) {
		this.ccAdds = ccAdds;
	}
	public String getMsg() {
		String msg ="";
		if(this.msg != null){
			msg = this.msg;
		}
		return msg;
	}
	public void setMsg(String msg) {
		this.msg = msg;
		this.htmlMail = this.isHtmlMsg();
	}
	
	private boolean isHtmlMsg(){
		final String regex = "<([a-zA-Z]\\w*?)>|</([a-zA-Z]\\w*?)>";
		boolean isHtml = false;
		if(this.msg != null && !this.msg.equals("")){
			Pattern patt = Pattern.compile(regex);
		    Matcher matcher = patt.matcher(this.msg);
			isHtml = matcher.find();
			

		}
		return isHtml;
	}
	public String toString(){
		String toString ="";
		 toString +="to:"; 
			 if(this.toAdds != null){
				 for(String adds : this.toAdds){
					 toString+=adds+"-";
				 }
			 }
		 toString +="bcc:";
		 if(this.bccAdds != null){
			 for(String adds : this.bccAdds){
				 toString+=adds+"-";
			 }
		 }
		 toString +="cc:";
		 if(this.ccAdds != null){
			 for(String adds : this.ccAdds){
				 toString+=adds+"-";
			 }
		 }
		 toString +="|subject ="+ this.subject+" ";
		 toString +="|msg ="+ this.msg+" ";
		 toString +="|attachments ="+ this.attachments+" ";
		 toString +="|htmlMail ="+ this.htmlMail+" ";
		 return toString;
	}
	public Mail() {
		super();
		
	}
	
	private static List<String> setStringProp(List<String> list, String value){
	
		if(list == null){
			list = new ArrayList<String>();
		}
		list.add(value);
		return list;
	}
}
